package com.yanqu.road.dao.impl.activity.unionchest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.twins.CrossTwinsBossData;
import com.yanqu.road.entity.activity.twins.CrossTwinsDamageRecordData;
import com.yanqu.road.entity.activity.twins.CrossTwinsUserData;
import com.yanqu.road.entity.activity.unionChest.CrossUnionChestBoxData;
import com.yanqu.road.entity.activity.unionChest.UnionChestBoxInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionChestBoxDaoImpl extends TempDataObjectDao<CrossUnionChestBoxData> {
    public static String getCrossUnionChestBoxDataSql = "select * from `t_c_unionchest_box` where `activityId`=?";

    public Map<String, Map<Long, CrossUnionChestBoxData>> getCrossUnionChestBoxDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Long, CrossUnionChestBoxData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUnionChestBoxDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionChestBoxData boxData = parse(rs);
                Map<Long, CrossUnionChestBoxData> unionIdMap = dataMap.get(boxData.getUnionId());
                if (null == unionIdMap) {
                    unionIdMap = new ConcurrentHashMap<>();
                    dataMap.put(boxData.getUnionId(), unionIdMap);
                }
                unionIdMap.put(boxData.getBoxId(), boxData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossUnionChestBoxDataSql = "update `t_c_unionchest_box` set boxType=?,shareUserId=?,shareTime=?,boxInfoMap=? where activityId=? and unionId=? and boxId=?";

    @Override
    protected boolean updateData(CrossUnionChestBoxData crossUnionChestBoxData) {
        crossUnionChestBoxData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestBoxData.getBoxType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestBoxData.getShareUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestBoxData.getShareTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossUnionChestBoxData.getBoxInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestBoxData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossUnionChestBoxData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestBoxData.getBoxId()));
        if (getDb().executeNoneQuery(updateCrossUnionChestBoxDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossUnionChestBoxDataSql = "insert into `t_c_unionchest_box`(activityId,unionId,boxId,boxType,shareUserId,shareTime,boxInfoMap) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionChestBoxData crossUnionChestBoxData) {
        crossUnionChestBoxData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestBoxData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossUnionChestBoxData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestBoxData.getBoxId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestBoxData.getBoxType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestBoxData.getShareUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestBoxData.getShareTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossUnionChestBoxData.getBoxInfoMap())));
        if (getDb().executeNoneQuery(insertCrossUnionChestBoxDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossUnionChestBoxData parseData(ResultSet rs) throws SQLException {
        CrossUnionChestBoxData crossUnionChestBoxData = new CrossUnionChestBoxData();
        crossUnionChestBoxData.setActivityId(rs.getInt("activityId"));
        crossUnionChestBoxData.setUnionId(rs.getString("unionId"));
        crossUnionChestBoxData.setBoxId(rs.getLong("boxId"));
        crossUnionChestBoxData.setBoxType(rs.getInt("boxType"));
        crossUnionChestBoxData.setShareUserId(rs.getLong("shareUserId"));
        crossUnionChestBoxData.setShareTime(rs.getLong("shareTime"));
        crossUnionChestBoxData.setBoxInfoMap(JSON.parseObject(rs.getString("boxInfoMap"), new TypeReference<ConcurrentHashMap<Integer, UnionChestBoxInfo>>() {
        }));

        return crossUnionChestBoxData;
    }
}
